package com.bytedance.common.plugin.mop;

import android.text.TextUtils;
import android.util.Log;

import com.bytedance.common.plugin.baseface.BasePlugin;
import com.bytedance.common.plugin.framework.PluginManager;
import com.bytedance.common.plugin.framework.model.Plugin;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.reflect.Reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MopImplCaller {
    private Object[] mArgs;
    private String mMethodName;
    private MopAgent mMopAgent;
    private List<BasePlugin> mTargets;
    private Class[] mTypes;

    protected MopImplCaller(StackTraceElement stackTraceElement, Class[] parameterTypes, Object[] args) {
        String packageName = null;
        this.mTypes = parameterTypes;
        this.mArgs = args;
        String className = stackTraceElement.getClassName();
        this.mMethodName = stackTraceElement.getMethodName();
        Log.d("Plugin", "MopImplCaller()::" + className + "." + mMethodName + "()");
        if (this.mMopAgent == null) {
            try {
                this.mMopAgent = MopAgentUtil.getMopAgentAnnotation(Class.forName(className), this.mMethodName, parameterTypes, new Object[0]);
            } catch (Exception e) {
            }
        }

        if (this.mMopAgent != null) {
            switch (this.mMopAgent.targetType()) {
                case Plugin: {
                    this.mTargets = new ArrayList(1);
                    String packageName1 = this.mMopAgent.packageName();
                    if ((TextUtils.isEmpty(packageName1)) && args != null && parameterTypes != null) {
                        packageName = (String) args[parameterTypes.length - 1];
                        if (Logger.debug()) {
                            Logger.d("Plugin", "get args packageName = " + ((packageName)));
                        }

                        this.mArgs = new Object[parameterTypes.length - 1];
                        System.arraycopy(args, 0, this.mArgs, 0, parameterTypes.length - 1);
                        if (Logger.debug()) {
                            Logger.d("Plugin", "get this.mArgs = " + this.mArgs.length);
                        }

                        this.mTypes = new Class[parameterTypes.length - 1];
                        System.arraycopy(parameterTypes, 0, this.mTypes, 0, parameterTypes.length - 1);
                        if (Logger.debug()) {
                            Logger.d("Plugin", "get this.mTypes = " + this.mTypes.length);
                        }
                    }
                    if (TextUtils.isEmpty((packageName))) {
                        return;
                    }

                    Plugin plugin = PluginManager.getPlugin(packageName);
                    if (plugin == null) {
                        return;
                    }

                    if (plugin.mBaseProxy == null) {
                        return;
                    }

                    this.mTargets.add(plugin.mBaseProxy.getPlugin());
                    return;
                }
                case AllPlugins: {
                    this.mTargets = new ArrayList();
                    Iterator v1 = PluginManager.getPlugins().iterator();
                    while (v1.hasNext()) {
                        Object obj = v1.next();
                        if (((Plugin) obj).mBaseProxy == null) {
                            continue;
                        }
                        this.mTargets.add(((Plugin) obj).mBaseProxy.getPlugin());
                    }
                    return;
                }
                default:
                    return;
            }
        }
    }

    protected void execAction() {
        if (this.mTargets != null) {
            Iterator<BasePlugin> iterator = this.mTargets.iterator();
            while (iterator.hasNext()) {
                this.execOneAction(iterator.next(), this.mMethodName, this.mTypes, this.mArgs);
            }
        }
    }

    protected List execFunc() {
        List result = new ArrayList();
        if (this.mTargets != null) {
            Iterator<BasePlugin> iterator = this.mTargets.iterator();
            while (iterator.hasNext()) {
                Object obj = this.execOneFunc(iterator.next(), this.mMethodName, this.mTypes, this.mArgs);
                if (obj == null) {
                    continue;
                }
                result.add(obj);
            }
        }
        return result;
    }

    protected Object execFuncSingle() {
        Object obj;
        if (this.mTargets != null) {
            Iterator<BasePlugin> iterator = this.mTargets.iterator();
            do {
                if (iterator.hasNext()) {
                    obj = this.execOneFunc(iterator.next(), this.mMethodName, this.mTypes, this.mArgs);
                    if (obj == null) {
                        continue;
                    }
                } else {
                    return null;
                }
                return obj;
            } while (true);
        } else {
            obj = null;
        }
        return obj;
    }

    private void execOneAction(Object target, String methodName, Class[] types, Object[] args) {
        Method method;
        try {
            method = Reflect.onClass(target.getClass()).exactMethod(methodName, types);
            if (method == null) {
                return;
            }

            if (method.getAnnotation(MopAgent.class) != null) {
                return;
            }

            if (!this.mMopAgent.ignoreMopImpl()) {
                if (method.getAnnotation(MopImpl.class) == null) {
                    return;
                }
            }
            method.invoke(target, args);
        } catch (InvocationTargetException e) {
        } catch (IllegalAccessException e) {
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    private Object execOneFunc(Object target, String methodName, Class[] types, Object[] args) {
        Method method;
        try {
            method = Reflect.onClass(target.getClass()).exactMethod(methodName, types);
            if (method == null) {
                return null;
            }

            if (method.getAnnotation(MopAgent.class) != null) {
                return null;
            }

            if (!this.mMopAgent.ignoreMopImpl()) {
                if (method.getAnnotation(MopImpl.class) == null) {
                    return null;
                }
            }

            Object obj = method.invoke(target, args);
            return obj;
        } catch (InvocationTargetException e) {
        } catch (IllegalAccessException e) {
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }
}

